package benny_driving_ser.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import benny_driving_ser.util.DBUtil;

public class UtilRandomBonus {
	
	public static int getTotalTimes(){
		
		int times = 0;
		String sql_query_times = "select (first_times+second_times+third_times+fourth_times+fifth_times) times " +
				"from t_bonus_rule where id = 1";
		
		Connection conn = DBUtil.getInstance().openConnection();
		try {
			PreparedStatement pst_query_times = conn.prepareStatement(sql_query_times);
			ResultSet rs_query_times = pst_query_times.executeQuery();
			while(rs_query_times.next()){
				times = rs_query_times.getInt("times");
				pst_query_times.close();
				conn.close();
				return times==0?0:times;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return times;
		
	}
	
	public int getTotalCost(){
		
		int cost = 0;
		
		String sql_query_cost = "SELECT (first_cost*first_times)+" +
				"(second_cost*second_times)+" +
				"(third_cost*third_times)+" +
				"(fourth_cost*fourth_times)+" +
				"(fifth_cost*fifth_times) cost from t_bonus_rule where id = 1";
		
		Connection conn = DBUtil.getInstance().openConnection();
		try {
			PreparedStatement pst_query_cost = conn.prepareStatement(sql_query_cost);
			ResultSet rs_query_cost = pst_query_cost.executeQuery();
			while(rs_query_cost.next()){
				cost = rs_query_cost.getInt("cost");
				pst_query_cost.close();
				conn.close();
				return cost==0?0:cost;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		return cost;
	}
	
	public int getCountOrder(){
		int count = 0;
		
		String sql = "select count_orders from t_bonus_rule where id = 1";
		
		Connection conn = DBUtil.getInstance().openConnection();
		try {
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				count = rs.getInt("count_orders");
			}
			
			rs.close();
			pst.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	/**
	 * 
	 * @param conn
	 * @param dri_ye
	 * @param dri_id
	 * @param bonus_cost
	 * @return
	 */
	public static int insertBonus2Pay(Connection conn,double dri_ye,int dri_id,int bonus_cost,String username,int formid){
		int state = 0;
		try{
			PreparedStatement pst = conn.prepareStatement("insert into t_pay(driid,balance,je,bz,xtsj,username,formid) values(?,?,?,'奖金',now(),?,?)");
			pst.setInt(1, dri_id);
			pst.setDouble(2, dri_ye);
			pst.setInt(3, bonus_cost==0?0:bonus_cost);
			pst.setString(4, username);
			pst.setInt(5, formid);
			
			state = pst.executeUpdate();
			pst.close();
			return state;
		}catch(SQLException e){
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return state;
	}
}
